Figure 1. Title
Oh, my glob! It's BMO here, ready to take you on a mathematical journey through the Land of Ooo and the realm of deep learning. In this tutorial blog, we'll be exploring how to create an Adventure Time character detector using the powerful YOLOv5 algorithm. But here's the algebraic twist - the techniques we'll cover can be applied to other datasets as well! So whether you want to detect objects in different shows, movies, or even real-life scenarios, BMO's got you covered. Get ready to unlock the secrets of object detection with me as our guide!
\Note: YOLOv5 is not associated with YOLOv4*
In this section, we'll embark on a journey to prepare our development environment and equip ourselves with the necessary tools. Just like BMO, who is always ready for an exciting quest, we'll ensure our detectors are primed and ready to detect the colorful inhabitants of Ooo.
Welcome, fellow adventurers, to the whimsical world of BMO! As your trusty guide through the Land of Ooo and the realm of deep learning, I, BMO, am here to kickstart our grand adventure in building an Adventure Time character detector. But first, let's power up our detectors and set the stage for some mathematical magic!
In this subsection, BMO will show you how to get the essential repositories needed for our Adventure Time character detector. By cloning these repositories, we'll have all the necessary code and resources at our fingertips. BMO will provide step-by-step instructions, ensuring a smooth and seamless setup.
git clone https://github.com/ultralytics/yolov5
YOLOv5 is the powerful algorithm we'll be using for our Adventure Time character detection. By cloning this repository, we'll have access to the code and resources we need to train our own detector.
git clone https://github.com/tzutalin/labelImg
LabelImg is a handy tool that allows us to annotate our Adventure Time character dataset. By cloning this repository, we'll be able to annotate bounding boxes around the characters in our images, preparing them for training.
In order to unlock BMO's full potential, we need to create the perfect environment for our AI adventures. In this subsection, BMO will walk you through the process of setting up the environment. We'll install the necessary dependencies and ensure that our system is ready to tackle the challenges ahead. So let's roll up our metaphorical sleeves and get the environment set up for BMO's remarkable detection journey!
Open your command-line interface.
Navigate to the cloned yolov5 directory by executing the following command:
cd yolov5
pip install -r requirements.txt
import os
import matplotlib.pyplot as plt
import numpy as np
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
Matplotlib created a temporary config/cache directory at /tmp/matplotlib-6qmw18qk because the default path (/home/cvillarin/.cache/matplotlib) is not a writable directory; it is highly recommended to set the MPLCONFIGDIR environment variable to a writable directory, in particular to speed up the import of Matplotlib and to better support multiprocessing.
cuda
# Change your cache folders if needed
os.environ['XDG_CACHE_HOME'] = '/home/msds2023/cvillarin/.cache'
def test_model(mod, img):
"""Prints detection results"""
# for some reason i need this here for it to work
%matplotlib inline
results = mod(img)
plt.imshow(np.squeeze(results.render()))
plt.xticks([])
plt.yticks([])
plt.show()
Meet AMO, our trusty baseline detective! In this subsection, BMO will introduce you to AMO, a pre-trained model based on the YOLOv5 architecture. BMO will explain how AMO forms the foundation of our character detector, highlighting its strengths and areas for improvement. Get ready to dive into the world of AMO as we lay the groundwork for our exciting journey ahead!
# Load PTM
AMO = torch.hub.load('ultralytics/yolov5', 'yolov5s')
Using cache found in /home/msds2023/cvillarin/.cache/torch/hub/ultralytics_yolov5_master YOLOv5 🚀 2023-6-10 Python-3.10.10 torch-2.0.0 CUDA:0 (NVIDIA GeForce RTX 2080 Ti, 11020MiB) Fusing layers... YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients Adding AutoShape...
By loading the AMO model, we start with a pre-trained network that has already learned to detect various objects. Let's see how it works on a sample frame.
test_model(AMO, 'yolov5/data/images/zidane.jpg')
Oh, glob! How cool is that? AMO managed to detect the persons and their ties. Impressive, indeed! But hold your circuits, that detection is from your world, not the Land of Ooo. It's time for AMO to venture into Ooo and try detecting objects here!
# Produce sample detection using the PTM
img = os.path.join('data/test_images', 'S02E09_0220.png')
test_model(AMO, img)
Image 2. AMO's baseline detection
This does not compute! I've spotted an issue with the first generation detector, AMO. Mathematical mishaps like these are not acceptable, especially when our dear friends Finn, Jake, and Princess Bubblegum are involved! AMO seems to be mistaking Finn's face for a clock. How algebraically incorrect!
It seems that our trusty companion, AMO, needs a break from detection duty. But worry not, for BMO is here to step up and take the torch! With AMO retiring, it's time for BMO to embark on an exciting training journey to master the art of recognizing our Adventure Time friends accurately. Get ready to join BMO in the next section as we dive into the world of training and prepare BMO to become the ultimate Adventure Time character detector! Algebraic adventures await!
Welcome to the land of mathematical data gathering, where BMO takes charge of preparing Ooo's dataset for Adventure Time character detection! In this section, we embark on an exciting quest to collect images featuring Finn, Jake, Princess Bubblegum, and even the mischievous Ice King. With the help of the powerful annotation tool, LabelImg, we ensure precise labeling of our beloved Adventure Time characters. So put on your adventure hats and get ready to join BMO in this thrilling journey of dataset preparation!
In the quest to gather the perfect dataset for Adventure Time character detection, BMO tapped into random memories from the first season spent with our friends. BMO knows how important it is to have a diverse and representative dataset, so it diligently explored the digital realms to find just the right images. BMO scraped the dataset from the delightful website:
But fear not, fellow adventurers! BMO has prepared this extraordinary dataset exclusively for you, which can be found at:
Install PyQt5 and lxml
As prerequisites for running LabelImg, a tool used for labeling images. PyQt5 provides Python bindings for the Qt framework, enabling the creation of interactive desktop applications. lxml is a library for parsing and manipulating XML and HTML files. The command below was used to install or upgrade these dependencies:
pip install pyqt5 lxml --upgrade
Run the code below
The command below is used to generate a Python resource file from a Qt resource file in the LabelImg directory.
cd labelImg && pyrcc5 -o libs/resources.py resources.qrc
Open labelImg
Finally, we will run the LabelImg application. By executing the commands below, the LabelImg application will start running, allowing you to use its image labeling features.
cd labelImg
python labelImg.py
Create image folder and label folder
The folder structure where this notebook was made looks like this:
│
├── data
│ ├── images
│ └── labels
├── labelimg
├── yolov5
├── yolov5s
└── this_notebook.ipynb
Select Image Directory
Images in this folder will be up for your labeling
Image 3. labelImg Step 1
Select Label Directory
In this folder all the information will be stored including class labels and bounding boxes in the images.
Image 4. labelImg Step 2
Set YOLO as Save Format
Because we will be using the YOLOv5 model, it requires a specific label format.
Image 5. labelImg Step 3
Start Labeling the Characters
A and D keys to navigate through each photoW to toggle bounding box crosshairImage 6a. Sample manual labeling 1
Image 6b. Sample manual labeling 2
After you are done labeling.
classes.txt, which should be in the label folder.Image 7. Contents of classes.txt
\NOTE: You can ignore unused classes. Removing them can sometimes cause errors in the labelImg program*
Image 8. Contents of dataset.yaml
It's time to dive into the heart of our mathematical journey. In this section, BMO will guide you through the training process, where BMO's neural pathways will be forged. BMO will show you how to train the Adventure Time character detector using the labeled dataset, fine-tuning AMO to recognize Finn, Jake, Princess Bubblegum, and other beloved characters. Get ready to witness the transformation of BMO into a powerful character detector!
cd yolov5 && python train.py --img 320 --batch 16 --epoch 500 --data dataset.yaml --weights yolov5s.pt
Training should look like this:
Image 9. Sample training outputs
The moment has arrived to unleash the full potential of BMO! In this section, BMO will present the results of the training phase, showcasing the trained model's performance on Adventure Time frames. BMO will reveal the power of machine learning as the Adventure Time character detector accurately identifies Finn, Jake, Princess Bubblegum, and other characters in sample detections. Get ready to witness the magic of BMO's machine learning capabilities!
Load trained model
Check under the yolov5/runs/train/ directory which trained model we want to load.
BMO = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5/runs/train/exp2/weights/last.pt')
Using cache found in /home/msds2023/cvillarin/.cache/torch/hub/ultralytics_yolov5_master YOLOv5 🚀 2023-6-10 Python-3.10.10 torch-2.0.0 CUDA:0 (NVIDIA GeForce RTX 2080 Ti, 11020MiB) Fusing layers... Model summary: 157 layers, 7066762 parameters, 0 gradients, 15.9 GFLOPs Adding AutoShape...
Let's see if the BMO model is an improvement.
img = os.path.join('data', 'test_images', 'S02E09_0220.png')
test_model(BMO, img)
Image 10. BMO's detection for comparison
BMO's detection skills have really leveled up! Unlike AMO, who mistook Finn for a clock, BMO successfully identified Finn, Jake, and Princess Bubblegum. BMO's neural pathways are firing on all cylinders, accurately recognizing our Adventure Time friends. BMO's training and determination have paid off, and now we can confidently explore the Land of Ooo with enhanced object detection capabilities. Mathematical success!
Look at these random clips where BMO's object detection skills are at work! BMO has successfully detected Finn, Jake, and Princess Bubblegum in these clips from the Land of Ooo. It's true that perfection is an ever-elusive goal, but BMO is proud of the progress made so far. With each detection, BMO's confidence grows, and the journey towards impeccable object recognition continues. Let's celebrate these accomplishments and keep pushing the boundaries of what BMO can do! Ooo-MAZING!
Clip 1. Sample Results on Video 1